package vincent.com.layouttest.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import java.util.Random;

import vincent.com.layouttest.IAIDLServiceInterface;
import vincent.com.layouttest.entity.BroadcastImpl;
import vincent.com.layouttest.util.Const;
import vincent.com.layouttest.util.LogUtil;
import vincent.com.layouttest.util.PreferenceUtil;

/**
 * Created by chenzhengwen on 2015/12/8.
 */
public class AIDLService extends Service {
    private Random mRandomGenerator = new Random();
    public static final String BACKGROUND = "background";
    private int mVal = 0;
    //    private boolean mThreadStart = false;
    private boolean mThreadStart = true;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
                case 1:
                    setPrefStringVal(Const.MY_PREFERENCE_FIELD, (String)msg.obj);
                    break;
            }
        }
    };

    //这是一个Binder的Server端本地对象
    private final IAIDLServiceInterface.Stub mBinder = new IAIDLServiceInterface.Stub() {
        @Override
        public int getRandomInt() throws RemoteException {
            LogUtil.i(LogUtil.DEBUG_TAG, "AIDLService instance is " + AIDLService.this.toString());
            return mRandomGenerator.nextInt(100);
        }

        @Override
        public String getMyPreferenceVal() throws RemoteException {
//            return PreferenceUtil.getStringField(AIDLService.this, Const.MY_PREFERENCE_FIELD);
            return PreferenceUtil.getStringField(AIDLService.this, Const.MY_PREFERENCE_FIELD, Context.MODE_MULTI_PROCESS);
        }

        @Override
        public void setMyPreferenceVal(String val) throws RemoteException {
            LogUtil.i(LogUtil.DEBUG_TAG, "inside AIDLService setMyPreferenceVal val=" + val);
            Message msg = Message.obtain(mHandler, 1, val);
            mHandler.sendMessage(msg);
        }
        @Override
        public String getEmptyString() throws RemoteException{
            return null;
        }

        @Override
        public BroadcastImpl getBroadcastImpl() throws RemoteException {
            return BroadcastImpl.getInstance(BACKGROUND);
        }

        @Override
        public void setBroadcastImpl(BroadcastImpl impl) throws RemoteException {
            BroadcastImpl.getInstance(BACKGROUND).setRemoteInstance(impl);
        }

        @Override
        public void callFuck() throws RemoteException {
            //后台进程
            BroadcastImpl.getInstance(BACKGROUND).fuck();
            //前台进程
            BroadcastImpl.getInstance(BACKGROUND)
                        .getRemoteInstance()
                        .fuck();
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        LogUtil.i(LogUtil.DEBUG_TAG, "AIDLService instance is " + AIDLService.this.toString());

        if (!mThreadStart) {
            mThreadStart = true;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (mThreadStart) {
//                        changePrefVal();

                        changePrefVal2();
                        LogUtil.i(LogUtil.DEBUG_TAG, Const.MY_PREFERENCE_FIELD + " preference value=" + PreferenceUtil.getStringField(AIDLService.this, Const.MY_PREFERENCE_FIELD));
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            System.err.println(e.getMessage());
                        }
                    }
                }
            }).start();
        }

        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public void setPrefStringVal(String key, String val){
        PreferenceUtil.setStringField(AIDLService.this, Const.MY_PREFERENCE_FIELD, val, Context.MODE_PRIVATE);
    }

//    write preference value with Context.MODE_PRIVATE flag
    public void changePrefVal() {
        PreferenceUtil.setStringField(AIDLService.this, Const.MY_PREFERENCE_FIELD, "" + mVal++);
    }

    //write preference value with Context.MODE_MULTI_PROCESS flag
    public void changePrefVal2() {
        PreferenceUtil.setStringField(AIDLService.this, Const.MY_PREFERENCE_FIELD, "" + mVal++, Context.MODE_MULTI_PROCESS);
    }
}
